﻿using IdentityModel.Client;
using Microsoft.AspNetCore.Mvc;

namespace API.Controllers
{

    /// <summary>
    /// 获取Token
    /// </summary>
    [Route("api/[Controller]/[action]")]
    [ApiController]
    public class TokenController : ControllerBase
    {

        /// <summary>
        /// 获取客户端凭据Token
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetCredentialsToken()
        {
            var client = new HttpClient();
            //var disco = await client.GetDiscoveryDocumentAsync("http://localhost:7160");
            //if (disco.IsError)
            //{
            //    return Content(disco.Error);
            //}
            ////请求token
            //var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
            //{
            //    Address = disco.TokenEndpoint,
            //    ClientId = "client",
            //    ClientSecret = "secret",
            //    Scope = "client_scope2"
            //});

            var disco = await client.GetDiscoveryDocumentAsync("http://localhost:44397");
            if (disco.IsError)
            {
                return Content(disco.Error);
            }
            //请求token
            var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
            {
                Address = disco.TokenEndpoint,
                ClientId = "console-client-demo",
                ClientSecret = "1q2w3e*",//E5Xd4yMqjP5kjWFKrYgySBju6JVfCzMyFp7n2QmMrME=
                Scope = "IdentityService"
            });
            //1q2w3E*

            if (tokenResponse.IsError)
            {
                return Content(tokenResponse.Json.ToString());
                // Content(System.Text.Json.JsonSerializer.Serialize<TokenResponse>(tokenResponse));
            }
            return Content(tokenResponse.AccessToken);
        }

        /// <summary>
        /// 密码模式凭据获取
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetPassWordToken()
        {
            var client = new HttpClient();
            var disco = await client.GetDiscoveryDocumentAsync("http://localhost:44397");
            if (disco.IsError)
            {
                return Content(disco.Error);
            }
            //请求token
            var tokenResponse = await client.RequestPasswordTokenAsync(new PasswordTokenRequest
            {
                Address = disco.TokenEndpoint,
                ClientId = "console-client-demo",
                ClientSecret = "1q2w3e*",//E5Xd4yMqjP5kjWFKrYgySBju6JVfCzMyFp7n2QmMrME=
                Scope = "IdentityService",
                UserName = "admin",
                Password = "1q2w3E*"
            });
            //1q2w3E*

            if (tokenResponse.IsError)
            {
                return Content(tokenResponse.Json.ToString());
                // Content(System.Text.Json.JsonSerializer.Serialize<TokenResponse>(tokenResponse));
            }
            return Content(tokenResponse.AccessToken);
        }
    }
}
